package cn.qyd.kaoshi;

import java.util.ArrayList;

/**
 * @author 邱运铎
 * 直接找出能吃到最多的pizza是多少
 * @date 2024-03-26 22:22
 */
public class EatPizza {
    private static int[] pizza = new int[]{8,2,10,5,7};
    private static ArrayList<String> result = new ArrayList<>();
    private static StringBuilder sb = new StringBuilder();
    private static int num = 0;
    private static int max = 0;
    public static void main(String[] args) {
        int len = pizza.length;
        for (int i = 0; i < len; i++) {
            System.out.println(num);
            num += pizza[i];
            dispatchPizza(i -1, i + 1);
            num -= pizza[i];
        }
        System.out.println(max);
    }

    public static void dispatchPizza(int left, int right) {
        left = left < 0 ? pizza.length - 1  : left;
        right = right >= pizza.length ? 0 : right;
        //因为馋嘴必定是选择当前缺口左右两边最大的那个，比较那边大，让边界向那边移动一位，表示被馋嘴吃掉
        if (pizza[left] > pizza[right]) {
            left = left - 1 < 0 ? pizza.length - 1 : left - 1;
        } else {
            right = right + 1 >= pizza.length ? 0 : right + 1;
        }
        //如果被馋嘴吃掉后，left和right相等，表明剩下最后一块，直接让贪吃吃掉，并返回。
        if (left == right) {
            num += pizza[left];
//            System.out.println(num);
            max = Math.max(num, max);
            num -= pizza[left];
            return;
        }
        //让贪吃吃掉左边界的披萨
        num += pizza[left];
        left--;
        dispatchPizza(left,right);
        left++;
        num -= pizza[left];
        //让贪吃吃掉右边界的披萨
        num += pizza[right];
        right++;
        dispatchPizza(left,right);
        right--;
        num -= pizza[right];
    }
}

